#! encoding: utf-8
from goodies.models import Book, Category, Picture, CelebrateList
from goodies.models import Customer, Order 
from django.contrib import admin
from mptt.admin import MPTTModelAdmin
from sorl.thumbnail.admin import AdminImageMixin



#class PictureInLine(admin.StackedInline):
#    model = Picture
#    extra = 3


class PictureInLine(AdminImageMixin, admin.TabularInline):
    model = Picture

class BookinLine(admin.TabularInline):
    model  = Book
    extra  = 0
    #fk_name = 'books'
    fields = ('author','name','price', 'status','resolution') 

class CustomerBookinLine(admin.TabularInline):
    model  = Book
    extra  = 0
    fk_name = 'customer'
    fields = ('author','name','price', 'status','resolution') 
    

class CustomerInline(admin.StackedInline):
    model = Customer
    extra = 0

class OrderInline(admin.StackedInline):
    model   = Order
    
    
    extra = 0
     
  
     
  



class BookAdmin(admin.ModelAdmin):
    fieldsets = [
      (u'Описание',    {'fields':['author','name','place','publisher','year','pages','size','cover','description','safety','price']}),
      (u'Служебная информация',  {'fields':['status','storage','date','resolution','owner'],'classes':['grp-collapse grp-closed']}),
      (u'Категории',  {'fields':['categories'],'classes':['grp-collapse grp-closed']}),
    ]
    filter_horizontal = ['categories']

    inlines = [PictureInLine]
    list_display  = ('author','name','price','status','date','resolution')
    list_editable = ('name','price','status','date','resolution')
    list_filter  = ['categories','resolution','status','price']

    search_fields = ['author','name','description']


class OrderAdmin(admin.ModelAdmin):

    list_display = ['date','fio','status','total_price','phone','email','address']
    list_filter  = ['date','status']
    #fields       = ['fio,','status','comment','total_price','phone','email','address','pay_type','is_vip']
    exclude = ['customer']
    #readonly_fields = ('phone','email','address','pay_type','is_vip','comment')
    #list_select_related = True
    inlines = [BookinLine]





 
#    fio      = models.CharField(max_length=140,   blank=True  )#
#

#    phone    = models.CharField(max_length=12,    )
#    email    = models.CharField(max_length=140)
#    passw    = models.CharField(max_length=12)
#    address  = models.TextField()
#    pay_type = models.SmallIntegerField(choices=payment_type_choice, default=0)
#    is_vip   = models.NullBooleanField()
#    comment  = models.TextField(blank=True,null=True)#

#    date     = models.DateField(auto_now=True, null=True)
#    


        




class CustomerAdmin(admin.ModelAdmin):
    #fieldsets = ["fio", "phone", "email", "passw", "address", "pay_type", "is_vip", "comment"]
    list_display = ('fio','date')
    list_filter  = ['pay_type','is_vip']
    inlines = [OrderInline,CustomerBookinLine]
    

class CelebrateListAdmin(admin.ModelAdmin):
    raw_id_fields = ["books"]
    related_lookup_fields = {
        'm2m': ['books'],
    }









admin.site.register(CelebrateList, CelebrateListAdmin)
admin.site.register(Customer, CustomerAdmin)
admin.site.register(Order,OrderAdmin)
admin.site.register(Book, BookAdmin)
admin.site.register(Category,MPTTModelAdmin)
